<%@page import="cn.yunhe.util.PageUtil"%>
<%@page import="java.util.ArrayList"%>
<%@page import="cn.yunhe.biz.CateBizImpl"%>
<%@page import="cn.yunhe.biz.ICateBiz"%>
<%@page import="cn.yunhe.model.Cate"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
table {
	width: 50%;
	margin: 0 auto;
	text-align: center;
	border: 1px solid black;
	border-collapse: collapse;
}

#page, #con {
	margin: 0 auto;
	text-align: center;
}

#search {
	margin: 0 auto;
	width: 50%;
}
</style>

</head>
<script type="text/javascript" src="js/jquery-3.1.1.js"></script>


<script>
	$(function() {
		$("tr:even").css("background", "#cd5c5c");

	});

	function delCheck() {
		var flag = confirm("确认删除么?");
		if (flag) {
			return true;
		} else {
			return false;
		}
	}

	function changePage() {
		var pageNum = document.getElementById("pageNum").value;
		if (!isNaN(pageNum)) {
			location.href = "listcate.jsp?pageIndex=" + pageNum;
		} else {
			alert('白痴,输入阿拉伯数字!');
		}

	}
</script>

<body>
	<div id="con">
		<h2>
			<a href="index.jsp">返回首页</a> <a href="addCate.jsp">添加主题</a>

		</h2>
		<h2>主题列表</h2>

	</div>


	<%
		//获得页的索引
		String pageIndex = request.getParameter("pageIndex");

		if (pageIndex == null) {
			pageIndex = "1";
		}
		//当前页
		int currPage = Integer.parseInt(pageIndex);
		//每页显示的条数
		int pageSize = 3;

		ICateBiz cateBiz = new CateBizImpl();
		//总条数
		int count = cateBiz.getCount();

		//根据总条数和每页显示的条数,得到总页数
		int totalPage = PageUtil.getTotalPage(count, pageSize);
		if (currPage < 1) {
			currPage = 1;
		} else if (currPage > totalPage) {
			currPage = totalPage;
		}

		List<Cate> list = cateBiz.searchByPage(pageSize, currPage);

		Object obj = session.getAttribute("cates");
		session.removeAttribute("cates");
		if (obj != null)
			list = (List<Cate>) obj;
	%>
	<div id="search">
		<form method="post" action="SearchServlet">
			编号：<input name="cid" />&nbsp;&nbsp;主题名称:<input name="cname" />&nbsp;&nbsp;<input
				type="submit" value="搜索" />
		</form>
	</div>
	<table border="1">
		<tr>
			<th>编号</th>
			<th>主题</th>
			<th>操作</th>
		</tr>
		<%
			for (int i = 0; i < list.size(); i++) {
				Cate c = list.get(i);
		%>
		<tr>
			<td><%=c.getCateId()%></td>
			<td><%=c.getCateName()%></td>
			<td><a href="editCate.jsp?cateid=<%=c.getCateId()%>">修改</a>|<a
				href="delCate_action.jsp?cateid=<%=c.getCateId()%>"
				onclick="return delCheck()">删除</a></td>

		</tr>

		<%
			}
		%>


	</table>
	<br>
	<div id="page">
		<a href="listcate.jsp?pageIndex=1">首页</a>| <a
			href="listcate.jsp?pageIndex=<%=currPage - 1%>">上一页</a>| <a
			href="listcate.jsp?pageIndex=<%=currPage + 1%>">下一页</a>| <a
			href="listcate.jsp?pageIndex=<%=totalPage%>">末页</a>&nbsp;&nbsp; 跳到<input
			type="text" name="pageNum" id="pageNum" size="1">页 <input
			type="button" value="跳转" onclick="changePage()">
	</div>
</body>
</html>